/*
 * SFTPServer.h
 *
 *  Created on: Apr 6, 2011
 *      Author: sms103
 */

#ifndef SFTPSERVER_H_
#define SFTPSERVER_H_

class SFTPServer;

#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <map>
#include <pthread.h>
#include <iostream>
#include <string>

#include "SFTPServerListener.h"
#include "SFTPSessionHandler.h"
#include "../shared/ParamParser.h"

using namespace std;

#define DEFAULT_PARAM_FILE_NAME  "SFTP_SERVER_PARAMS.config"

class SFTPServer{
public:
	SFTPServer(const char *paramFile);

	/* get a session identified by sessionID. returns null if the session
	 * does not exist.
	 */
	SFTPSessionHandler *getSession(uint32_t sessionID);

	/* Assign an ID to a new session, add it to the map, and return
	 * the assigned sessionID.*/
	uint32_t addSession(SFTPSessionHandler *handler);

	/* deletes the specified session (removes it from the map) */
	void deleteSession(uint32_t sessionID);

	virtual ~SFTPServer();

	//config file parameter parser
	ParamParser *paramParser;

private:
	//map of active session keys and the handlers
	map<uint32_t,SFTPSessionHandler *> sessionMap;

	//must be locked before accessing sessionMap or the sessionID
	pthread_mutex_t serverMutex;

	//used as session ID for each new connection then incremented. randomly initialized.
	//wrapped to zero if max int is reached.
	uint32_t sessionIDCounter;

	//the listener object for this server. runs in its own thread.
	SFTPServerListener *listener;



	//initializes session ID to random 32bit int value
	void initSessionID();

	//blocks reading stdin until the user enters "exit"
	void waitForExit();
};

#endif /* SFTPSERVER_H_ */
